package com.briup.shopadmin.service.impl;

import com.briup.shopadmin.common.GlobalException;
import com.briup.shopadmin.common.bean.Shop;
import com.briup.shopadmin.dao.IShopDao;
import com.briup.shopadmin.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    IShopDao shopDao;
    @Override
    public Page findAllByName(String name,Integer pageNum,Integer pageSize) {
        if (pageNum<1||pageSize<1){
            throw new GlobalException(52001,"分页参数异常");
        }
        PageRequest request = PageRequest.of(pageNum-1, pageSize);
        Page<Shop> page = shopDao.findByNameContaining(name,request);
        return page;
    }

    @Override
    public void save(Shop shop) {
        shopDao.save(shop);
    }

    @Override
    public void updateShopStatus(long id) {
        Optional<Shop> optional = shopDao.findById(id);
        Shop shop = optional.get();
        shop.setStat(!shop.isStat());
        shopDao.save(shop);
    }

    @Override
    public Shop getShopInfo(long id) {
        return shopDao.findById(id).get();
    }
}